package de.acagamics.roteschlupfmuetze.core;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageFilter;
import java.awt.image.BufferedImageOp;
import java.util.LinkedList;
import java.util.Random;

import com.golden.gamedev.Game;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.SpriteGroup;
import com.golden.gamedev.util.ImageUtil;

import de.acagamics.roteschlupfmuetze.RoteSchlupfmuetze;

public class Player {
	
	private double posX = 100;
	private double posY = 100;
	private double destX = 100;
	private double destY = 100;
	private double delta = 10;
	private int width = 70;
	private int height = 140;
	private double movementSpeedX = 5;
	private double movementSpeedY = 8;
	
	private LinkedList<LevelObject> inventory;
	
	public int getInventorySize(){
		return inventory.size();
	}
	
	SpriteGroup playerSprites = new SpriteGroup("Player");
	private BufferedImage playerImage = null;
	private BufferedImage playerImageLeft = null;
	private Sprite playerSprite = null;
	private boolean direction = false;
	
	public Player() {
		inventory = new LinkedList<LevelObject>();
		playerImage = RoteSchlupfmuetze.loadImage(
				"de/acagamics/roteschlupfmuetze/player/character_complete.png");
		playerImageLeft = RoteSchlupfmuetze.loadImage(
				"de/acagamics/roteschlupfmuetze/player/character_complete_left.png");
		playerSprite = new Sprite(ImageHelper.scaleImage(playerImage, width, height), posX, posY);
		
		playerSprites.add(playerSprite);
	}
	
	public void resetInventory(){
		for(LevelObject lo : inventory){
			lo.getObjectSprite().setActive(false);
		}
		inventory.clear();
	}
	
	public void update(long elapsedTime) {
		// set sprite x and y
		
		updatePosition();
	}
	
	Random random = new Random();
	private void updatePosition() {
		// update pos variables
		posX = playerSprite.getX();
		posY = playerSprite.getY();
		if(Math.abs(posX - destX) > 3*delta) {
			double movement = destX<posX? -1 : 1;
			movement *= movementSpeedX;
			playerSprite.moveX(movement);
			if(direction)playerSprite.setImage(ImageHelper.scaleImage(playerImageLeft, width, height + random.nextInt(4)-2));
			else playerSprite.setImage(ImageHelper.scaleImage(playerImage, width, height + random.nextInt(4)-2));
		}else if (Math.abs(posX - destX) > delta){
			double movement = destX<posX? -1 : 1;
			movement *= (movementSpeedX / 2);
			playerSprite.moveX(movement);
			if(direction)playerSprite.setImage(ImageHelper.scaleImage(playerImageLeft, width, height + random.nextInt(4)-2));
			else playerSprite.setImage(ImageHelper.scaleImage(playerImage, width, height + random.nextInt(4)-2));
		}else {
			playerSprite.moveX(0);
			if(direction)playerSprite.setImage(ImageHelper.scaleImage(playerImageLeft, width, height));
			else playerSprite.setImage(ImageHelper.scaleImage(playerImage, width, height));
		}
		if(Math.abs(posY - destY) > 2*delta) {
			double movement = destY<posY? -1 : 1;
			movement *= movementSpeedY;
			playerSprite.moveY(movement);
		}else if (Math.abs(posY - destY) > delta){
			double movement = destY<posY? -1 : 1;
			movement *= (movementSpeedY / 2);
			playerSprite.moveY(movement);
		}else {
			playerSprite.moveY(0);
		}
	}

	
	// used to set the destination of the player movement
	public void moveToX(int x) {
		this.destX = x;
		if(this.destX+this.width > RoteSchlupfmuetze.WIDTH) {
			this.destX = RoteSchlupfmuetze.WIDTH - this.width;
		}
		if(destX>posX) {
			direction = false;
		}
		else{
			direction = true;
		}
	}
	
	public void moveToX(int x, boolean ignoreBorder) {
		this.destX = x;
		if(!ignoreBorder)
		if(this.destX+this.width > RoteSchlupfmuetze.WIDTH) {
			this.destX = RoteSchlupfmuetze.WIDTH - this.width;
		}
		if(destX>posX) {
			direction = false;
		}
		else{
			direction = true;
		}
	}
	
	// used to set the destination of the player movement
	public void moveToY(int y) {
		this.destY = y;
	}

	public Sprite getPlayerSprite() {
		return playerSprite;
	}

	public void setPlayerSprite(Sprite playerSprite) {
		this.playerSprite = playerSprite;
	}
	
	public SpriteGroup getPlayerSprites() {
		return playerSprites;
	}

	public void setPlayerSprites(SpriteGroup playerSprites) {
		this.playerSprites = playerSprites;
	}

	public double getMovementSpeedX() {
		return movementSpeedX;
	}

	public double getMovementSpeedY() {
		return movementSpeedY;
	}

	public void addItem(LevelObject item) {
		this.inventory.add(item);
	}
	
	public void stopPlayer() {
		destX = posX;
		destY = posY;
		playerSprite.move(0, 0);
	}
	
	public void stopPlayer(boolean block) {
		if(block){
			if(destX>posX) {
				posX -= delta;
			}
			else{
				posX += delta;
			}
		}
		destX = posX;
		destY = posY;
		playerSprite.move(0, 0);
	}
	
	public void useItem(LevelObject item) {
		//shift objects to fill empty space
		int removeIndex = inventory.indexOf(item);
		for (int i = removeIndex; i < getInventorySize(); i++){
			inventory.get(i).setPosY(inventory.get(i).getPosY()-80);
		}
		//remove
		this.inventory.remove(item);
	}

	public boolean isMoving() {
		return (posY != destY) || (posX != destX);
	}

}
